var up_doc = window.dialogArguments.document;
function $(name) {
	return document.getElementsByName(name);
}
function $2(name) {
	return up_doc.getElementsByName(name);
}
var writeSuccess = false;
/**
 * 去除左右空格
 */
function trim(str) {
	return str.replace(/^\s*/, "").replace(/\s*$/, "");
}

function getStringLength(str) {
	var len = 0;
	for (var i = 0; i < str.length; i++) {
		var c = str.charCodeAt(i);
		// 单字节加1
		if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
			len++;
		} else {
			len += 3;
		}
	}
	return len;
}

function checkInputLength() {
	for (var key in inputMap.inputLengthMap) {
		var length = getStringLength($(key)[0].value);
		if (length > inputMap.inputLengthMap[key]) {
			alert(inputMap.contrast[key].replace("my:", "") + "字符长度超过"
					+ inputMap.inputLengthMap[key]);
			return false;
		}
	}
	return checkAptitudeFieldsLength();
}
var repeatOperator;
// 资质管理的字段
var aptitudeFields = {
	fields : ["aptitudeClassSelect", "aptitudeClass",// "whetherToProvide",
			"startTime", "endTime", "pk_venapt"],
	map : {
		aptitudeClass : "my:供应商资质类别",
		// whetherToProvide : "my:是否提供",
		startTime : "my:开始时间",
		endTime : "my:到期时间",
		pk_venapt : "my:pk_venapt"
	},
	fieldslength : {
		aptitudeClass : 100,
		startTime : 20,
		endTime : 20
	}
};
// 检查资质管理的字段长度是否超过最大数
function checkAptitudeFieldsLength() {
	for (var key in aptitudeFields.fieldslength) {
		for (var i = 0; i < $(key).length; i++) {
			var length = getStringLength($(key)[i].value);
			if (length > inputMap.inputLengthMap[key]) {
				alert("第"(i + 1) + "行，字段“"
						+ inputMap.contrast[key].replace("my:", "") + "”字符长度超过"
						+ inputMap.inputLengthMap[key]);
				return false;
			}
		}
	}
	return true;
}
/**
 * 重置，将资质管理的行数为1行，并且字段置空
 */
function resetAptitude() {

	var length = $("aptitudeClassSelect").length;
	for (var i = 1; i < length; i++) {
		$("aptitudeClassSelect")[0].parentNode.parentNode.parentNode
				.removeChild($("aptitudeClassSelect")[0].parentNode.parentNode)
	}
	for (var i = 0; i < aptitudeFields.fields.length; i++) {
		$(aptitudeFields.fields[i])[0].value = "";
	}
	if (repeatOperator) {
		repeatOperator.hideOperateDiv1();
	}
	$("aptitudeClassSelect")[0].parentNode.parentNode.style.display = "none";

}
/**
 * 供应商资质管理字段不隐藏
 */
function showAptitude() {
	$("aptitudeClassSelect")[0].parentNode.parentNode.style.display = "";
}
/**
 * 将资质管理的输入行注册成可增加或减少的行
 */
function registerRepeatTR() {
	repeatOperator = new RepeateTableOperator();
	repeatOperator
			.addListeners($("aptitudeClassSelect")[0].parentNode.parentNode);
	repeatOperator.setBeforeDelTrOperate(function(trNode) {
				if (trNode.getElementsByTagName("INPUT")[1] != "") {
					return confirm("选中行为已经导入的供应商资质，请确认是否删除。")
				}
			});
}
/**
 * 供应商资质管理初始化
 */
function initAptitude() {
	registerRepeatTR();
	if ($("providerIsExist")[0].checked == true) {
		showAptitude();
		var length = $2(aptitudeFields.map.aptitudeClass).length;
		repeatOperator.addEmptyTR(
				$("aptitudeClassSelect")[0].parentNode.parentNode,
				($2(aptitudeFields.map.aptitudeClass).length - 1));

		for (var i = 0; i < length; i++) {
			for (var key in aptitudeFields.map) {
				if (key == "aptitudeClass") {
					setAptitudeClassValue(i,
							$2(aptitudeFields.map[key])[i].value);
				} else {
					$(key)[i].value = $2(aptitudeFields.map[key])[i].value;
				}
			}
		}
	}
}
/**
 * 给供应商资质类型字段赋值
 * 
 * @param {}
 *            index
 * @param {}
 *            value
 */
function setAptitudeClassValue(index, value) {
	$("aptitudeClassSelect")[index].value = value;
	if ($("aptitudeClassSelect")[index].value != value) {
		$("aptitudeClassSelect")[index].value = "其他";
		onAptitudeClassSelectClick($("aptitudeClassSelect")[index]);
	}
	$("aptitudeClass")[index].value = value;
}
/**
 * 当更改供应商资质类型时
 * 
 * @param {}
 *            select
 */
function onAptitudeClassSelectClick(select) {
	if (select.value == "其他") {
		select.nextSibling.value = "";
		select.nextSibling.style.display = "";
	} else {
		select.nextSibling.value = select.value;
		select.nextSibling.style.display = "none";
	}
}
var dateInput;
/**
 * 显示日期选择
 * 
 * @param {}
 *            input
 */
function showDateSelect(input) {
	if (input && input.tagName == "INPUT") {
		dateInput = input;
	}
	input = dateInput;
	if (input.parentNode.parentNode.getElementsByTagName("INPUT")[1].value != "") {
		alert("不能变更已经导入的供应商资质信息，\n如需变更，请填写供应商信息变更申请表");
	} else {
		WdatePicker({
					readOnly : true
				});
	}
}
/**
 * 写入到表单的供应商资质管理
 */
function writeFormApptitude() {
	var length = $("aptitudeClassSelect").length;
	var count = length - $2(aptitudeFields.map.aptitudeClass).length;
	for (var i = 0; i < Math.abs(count); i++) {
		getParentTR($2(aptitudeFields.map.aptitudeClass)[0]).id = "oper";
		if (count > 0) {
			window.dialogArguments.addEmpty();
			getParentTR($2(aptitudeFields.map.aptitudeClass)[0]).id = "";
		} else
			window.dialogArguments.del();
	}

	for (var i = 0; i < length; i++) {
		for (var key in aptitudeFields.map) {
			$2(aptitudeFields.map[key])[i].value = $(key)[i].value;
		}
	}
}
/**
 * 从服务器上获取的供应商资质管理的数据，展示给用户
 * 
 * @param {}
 *            apptitude
 */
function showApptitudeValues(apptitude) {
	resetAptitude();
	showAptitude();
	if (apptitude) {
		var length = apptitude.length;
		repeatOperator.addEmptyTR(
				$("aptitudeClassSelect")[0].parentNode.parentNode, length - 1);
		for (var i = 0; i < length; i++) {
			for (var key in aptitudeFields.map) {
				if (key == "aptitudeClass") {
					setAptitudeClassValue(i, apptitude[i][key]
									? apptitude[i][key]
									: "");
				} else {
					$(key)[i].value = apptitude[i][key]
							? apptitude[i][key]
							: "";
				}
			}
		}
	}
}

/**
 * 检查供应商名称是否符合规定,并把小写字母改成大写，把全角字符改成半角
 * 
 * @param field
 * @returns {Boolean}
 */
function checkAndReplaceProviderName(field) {
	var isRight = false;

	var charlist = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	charlist += "abcdefghijklmnopqrstuvwxyz";
	charlist += "0123456789";
	charlist += " `~!@#$%^&*()_+-={}|[]\:\\\";'<>?,./";// 半角空格

	var providerName = field.value;
	var result = "";
	for (var i = 0; i < providerName.length; i++) {
		var c1 = providerName.charAt(i);
		var c2 = providerName.charCodeAt(i);
		if ("　" == c1) {
			result += " ";
		} else if ("，" == c1) {
			result += ",";
		} else if ("。" == c1) {
			result += ".";
		} else {
			if (charlist.indexOf(String.fromCharCode(c2 - 65248)) > -1) {
				result += String.fromCharCode(c2 - 65248);
			} else {
				result += c1;
			}
		}
	}

	field.value = trim(result.toUpperCase());
	isRight = true;

	return isRight;
}

// 已存在供应商供货范围
var providerIsExistRange = {
	// 合格供应商
	qualifiedSuppliers : null,
	potentialSuppliers : null,
	stopSuppliers : null
};

var isFillCode = function() {
	var e = up_doc.getElementById("my:表单控件选择2");
	return e && e.value == 'code';
};
var typeTree;
var companyTree;
var providerIsExist;
var ccontentValueArray = ["出厂价", "到库价", "FOB", "CIF", "C&F", "其他"];
var ccuspaycondValueArray = ["票后30天", "票后45天", "票后60天", "票后70天", "其他"];
var isExistInArray = function(array, value) {
	for (var i in array) {
		if (array[i] == value) {
			return i;
		}
	}
	return -1;
};

var setCcontentAndCcuspaycondDisabled = function() {
	$('ccontent')[0].value = '';
	$('ccuspaycond')[0].value = '';
	document.getElementById("ccontentDiv").style.display = 'none';
	document.getElementById("ccuspaycondDiv").style.display = 'none';
	$("ccontentSelect")[0].selectedIndex = -1;
	$("ccuspaycondSelect")[0].selectedIndex = -1;

};
var setCcontentAndCcuspaycond = function() {
	var ccontentValue = $('ccontent')[0].value;
	var ccuspaycondValue = $('ccuspaycond')[0].value;
	var ccontentIndex = (ccontentValue == '' ? 0 : isExistInArray(
			ccontentValueArray, ccontentValue));
	var ccuspaycondIndex = (ccuspaycondValue == '' ? 0 : isExistInArray(
			ccuspaycondValueArray, ccuspaycondValue));

	if (ccontentValue == '') {
		$('ccontent')[0].value = ccontentValueArray[0];
	}

	if (ccuspaycondValue == '') {
		$('ccuspaycond')[0].value = ccuspaycondValueArray[0];
	}

	if (ccontentIndex == -1) {
		ccontentIndex = 5;
		document.getElementById("ccontentDiv").style.display = '';
	} else {
		document.getElementById("ccontentDiv").style.display = 'none';
	}

	if (ccuspaycondIndex == -1) {
		ccuspaycondIndex = 4;
		document.getElementById("ccuspaycondDiv").style.display = '';
	} else {
		document.getElementById("ccuspaycondDiv").style.display = 'none';
	}
	$("ccontentSelect")[0].value = ccontentIndex;
	$("ccuspaycondSelect")[0].value = ccuspaycondIndex;
};

var initSupplyRangeType = function() {
	typeTree = new dhtmlXTreeObject("supplyRangeDIV", "100%", "100%", 0);
	typeTree.setSkin("csh_dhx_skyblue");
	typeTree.setImagePath("./apps_res/oa2uap/js/tree/imgs/csh_dhx_skyblue/");
	typeTree.enableCheckBoxes(1);
	typeTree.enableThreeStateCheckboxes(true);
	typeTree.loadXMLString(typeXML);
};
var createCompanyTree = function() {
	companyTree = new dhtmlXTreeObject("companyTreeDIV", "100%", "100%", 0);
	companyTree.setSkin("csh_dhx_skyblue");
	companyTree.setImagePath("./apps_res/oa2uap/js/tree/imgs/csh_dhx_skyblue/");
	companyTree.enableCheckBoxes(1);
	companyTree.enableThreeStateCheckboxes(true);
	companyTree.loadXMLString(companyTreeXML);
};
var onOpinionRadioClick = function() {
	setNodeCheckWhenProviderIsExist();
};
var setNodeCheckWhenProviderIsExist = function() {
	for (var nodeID in typeJSON) {
		if (typeJSON[nodeID].isleaf == 'Y') {
			typeTree.disableCheckbox(nodeID, 0);
		}
	}
	if ($("providerIsExist")[1].checked && !up_doc.getElementById("my:总办填写表单_")) {
		if ($("opinion")[0].checked) {
			typeTree.setCheck('0', false);
			for (var nodeID in typeJSON) {
				if (typeJSON[nodeID].isleaf == 'Y') {
					typeTree.disableCheckbox(nodeID, 0);
				}
			}
			for (var i in providerIsExistRange.qualifiedSuppliers) {
				typeTree.disableCheckbox(
						providerIsExistRange.qualifiedSuppliers[i], 1);
			}
			for (var i in providerIsExistRange.potentialSuppliers) {
				typeTree.disableCheckbox(
						providerIsExistRange.potentialSuppliers[i], 1);
			}
		} else if ($("opinion")[1].checked) {
			typeTree.setCheck('0', false);
			for (var nodeID in typeJSON) {
				if (typeJSON[nodeID].isleaf == 'Y') {
					typeTree.disableCheckbox(nodeID, 1);
				}
			}
			for (var i in providerIsExistRange.potentialSuppliers) {
				typeTree.disableCheckbox(
						providerIsExistRange.potentialSuppliers[i], 0);
			}
		}
		for (var i in providerIsExistRange.stopSuppliers) {
			typeTree.disableCheckbox(providerIsExistRange.stopSuppliers[i], 1);
		}
	}
};
var initSupplyNodeChecked = function() {
	var type1Array = $2(inputMap.supplyRange[0][1]);
	var type2Array = $2(inputMap.supplyRange[1][1]);
	var type3Array = $2(inputMap.supplyRange[2][1]);
	for (var index = 0; index < type3Array.length; index = index + 1) {
		if (type3Array[index].value === "") {
			if (type2Array[index].value === "") {
				if (type1Array[index].value !== "") {
					typeTree.setCheck(type1Array[index].value, true);
				}
			} else {
				typeTree.setCheck(type2Array[index].value, true);
			}
		} else {
			typeTree.setCheck(type3Array[index].value, true);
		}
	}
};
var initImportCompanyNodeChecked = function() {
	if (up_doc.getElementById("my:总办填写表单_")) {
		var idArray = $2(inputMap.importCompany[0][1]);
		for (var index = 0; index < idArray.length; index = index + 1) {
			if (idArray[index].value !== "") {
				companyTree.setCheck(idArray[index].value, true);
			}
		}
	} else {
		companyTree
				.setCheck(up_doc.getElementById("my:单位选择器")['default'], true);
		var childNodes = companyTree.getAllSubItems('0').split(',');
		for (var i in childNodes) {
			companyTree.disableCheckbox(childNodes[i], 1);
		}
	}
};
var writeSupplyRangeToForm = function() {
	var nodes = typeTree.getAllChecked().split(",");
	var typeArray = [];
	// -8924145959034269856机械制造

	if (up_doc.getElementById("my:单位选择器")['default'] != "-8924145959034269856") {
		$("groupManage")[0].checked = false;
		$("groupManage")[1].checked = true;
	}
	$2("my:是否需总裁审批")[0].checked = false;
	$2("my:是否需总裁审批")[1].checked = true;
	for (var index in nodes) {
		var nodeID = nodes[index];
		if (nodeID === "" || nodeID === null) {
			typeArray.push([["", ""], ["", ""], ["", ""]]);
		} else {
			if (up_doc.getElementById("my:单位选择器")['default'] != "-8924145959034269856") {
				if (typeJSON[nodeID].binvgroupdot == "Y") {
					$("groupManage")[0].checked = true;
					$("groupManage")[1].checked = false;
				}
			}
			if (typeJSON[nodeID].presidentagreedto == "Y") {
				$2("my:是否需总裁审批")[0].checked = true;
				$2("my:是否需总裁审批")[1].checked = false;
			}

			if (typeTree.hasChildren(nodeID) === 0) {
				var level = typeTree.getLevel(nodeID);
				if (level == 3) {
					var parentID = typeTree.getParentId(nodeID);
					typeArray.push([getTreeParentNodeArray(parentID),
							getTreeParentNodeArray(nodeID),
							[typeTree.getItemText(nodeID), nodeID]]);
				} else {
					if (level == 2) {
						typeArray.push([getTreeParentNodeArray(nodeID),
								[typeTree.getItemText(nodeID), nodeID],
								["", ""]]);
					} else {
						if (level == 1) {
							typeArray.push([
									[typeTree.getItemText(nodeID), nodeID],
									["", ""], ["", ""]]);
						}
					}
				}
			}
		}
	}
	writeCheckedNodeTextToFrom(typeArray, inputMap.supplyRange);
};
var getTreeParentNodeArray = function(nodeID) {
	var parentID = typeTree.getParentId(nodeID);
	var text = typeTree.getItemText(parentID);
	return [text, parentID];
};
var writeImportCompanyNodeToForm = function() {
	if (up_doc.getElementById("my:总办填写表单_")) {
		var nodes = companyTree.getAllChecked().split(",");
		var companyArray = [];
		for (var index in nodes) {
			var nodeID = nodes[index];
			if (nodeID === "" || nodeID === null) {
				companyArray.push([["", ""]]);
			} else {
				var level = companyTree.getLevel(nodeID);
				if (level === 1) {
					companyArray
							.push([[companyTree.getItemText(nodeID), nodeID]]);
				}
			}
		}
		writeCheckedNodeTextToFrom(companyArray, inputMap.importCompany);
	}
};
var writeCheckedNodeTextToFrom = function(values, repeatTableNames) {

	var parentDom = getTRParentNode(up_doc
			.getElementsByName(repeatTableNames[0][0])[0]);
	var childNodesLength = parentDom.childNodes.length;
	var valuesLength = values.length;

	if (childNodesLength > valuesLength) {
		for (var i = 0; i < (childNodesLength - valuesLength); i = i + 1) {
			parentDom.lastChild.id = 'oper';
			window.dialogArguments.del();
			parentDom.lastChild.id = "";
		}

		for (var index in values) {
			var rowValues = values[index];
			for (var rowIndex in rowValues) {
				$2(repeatTableNames[rowIndex][0])[index].value = rowValues[rowIndex][0];
				$2(repeatTableNames[rowIndex][1])[index].value = rowValues[rowIndex][1];
			}
		}
	} else {
		for (var index in values) {
			var rowValues = values[index];
			if (index < childNodesLength) {
				for (var rowIndex in rowValues) {
					$2(repeatTableNames[rowIndex][0])[index].value = rowValues[rowIndex][0];
					$2(repeatTableNames[rowIndex][1])[index].value = rowValues[rowIndex][1];
				}
			} else {
				var lastNode = parentDom.lastChild;
				lastNode.id = "oper";
				window.dialogArguments.addEmpty();
				lastNode.id = "";
				for (var rowIndex in rowValues) {
					$2(repeatTableNames[rowIndex][0])[index].value = rowValues[rowIndex][0];
					$2(repeatTableNames[rowIndex][1])[index].value = rowValues[rowIndex][1];
				}
			}
		}
	}

};
var initValue = function() {
	var contrast = inputMap.contrast;
	$("providerIsExist")[1].checked = radio1Checked = up_doc
			.getElementsByName(contrast["providerIsExist"])[0].checked;
	$("providerIsExist")[0].checked = radio0Checked = up_doc
			.getElementsByName(contrast["providerIsExist"])[1].checked;

	if (up_doc.getElementById("my:单位选择器")['default'] == "-8924145959034269856") {
		document.getElementById("groupManageTR").style.display = "";
	}

	if (radio0Checked || radio1Checked) {
		onSupplierTypeRadioClick();
	}
	if (radio1Checked) {
		$("providerCode")[0].value = up_doc
				.getElementsByName(contrast["providerCode"])[0].value;
	} else {
		for (var key in contrast) {
			if (key != "providerIsExist") {
				if (typeof contrast[key] == "string") {
					if (key == 'property') {
						$(key)[0].value = (up_doc
								.getElementsByName(contrast[key])[0].value == 'D'
								? '0'
								: '1');
					} else {
						$(key)[0].value = up_doc
								.getElementsByName(contrast[key])[0].value;
					}
				} else {
					if (typeof contrast[key] == "object") {
						if ($(key)[0].tagName.toUpperCase() == "SELECT") {
							$(key)[0].value = up_doc
									.getElementsByName(contrast[key][1])[0].value;
						} else {
							if (key == "opinion" || key == "groupManage") {
								$(key)[0].checked = up_doc
										.getElementsByName(contrast[key][1])[0].checked;
								$(key)[1].checked = up_doc
										.getElementsByName(contrast[key][1])[1].checked;
							}
						}
					}
				}
			}
		}
		initSupplyNodeChecked();
		initImportCompanyNodeChecked();
		initAptitude();
	}
	setCcontentAndCcuspaycond();
};
var getTRParentNode = function(node) {
	var tempNode = node;
	while (true) {
		if (tempNode && tempNode.tagName.toUpperCase() != "TR") {
			tempNode = tempNode.parentNode;
		} else {
			break;
		}
	}
	return tempNode.parentNode;
};
window.onload = function() {
	// $2("my:供货范围分类1")[0].parentNode.parentNode.parentNode.id='oper';
	// window.dialogArguments.add();
	var sign = false;
	if (isFillCode()) {
		if (operator == 1) {
			sign = true;
			document
					.write('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body style="margin-top: 50px; font-family: 微软雅黑; font-size: 10.5pt;"><table align="center" style="width: 280px"><tr><td width="130px" >供应商编码:</td><td><input id="code" type="text" width="100%" onkeydown="onEnterKeyPress()" style="font-family: font-family :   Arial; font-size: 10.5pt; color: #333399;; font-size: 10.5pt;"></td></tr><tr><td width="100px"></td><td align="right"><input id="button" type="button" value="确定" onclick="onButtonClick()" style="FONT-SIZE: 8pt; WIDTH: 60px; FONT-FAMILY: 微软雅黑"></td></tr></table></body><script src="./apps_res/oa2uap/js/checkPrivoiderCode.js"></script></html>');
		}
	} else {
		if (typeXML == "") {
			alert("未读取到供货范围，请联系ＯＡ系统管理员");
		}
		initSupplyRangeType();
		createCompanyTree();
		initValue();
	}

	if (!sign) {
		if (typeTree) {
			for (var nodeID in typeJSON) {
				if (typeJSON[nodeID].isleaf != 'Y') {
					typeTree.disableCheckbox(nodeID, 1);
				}
			}
		}
		if (!up_doc.getElementById("my:总办填写表单_")) {
			companyTree.setCheck(up_doc.getElementById("my:单位选择器")['default'],
					true);
			var childNodes = companyTree.getAllSubItems('0').split(',');
			for (var i in childNodes) {
				companyTree.disableCheckbox(childNodes[i], 1);
			}
		}
	}
	if ($("cVenBankMemo") && $("cVenBankMemo")[0])
		$("cVenBankMemo")[0].maxLength = 40;
};

var providerNameIsExist = function() {

};

var onButtonClick = function() {
	var providerName = $("providerName")[0].value;

	if (!providerName || providerName == "") {
		alert("供应商名称不能为空");
		return;
	}

	if (!checkAndReplaceProviderName($("providerName")[0])) {
		return;
	}
	providerName = $("providerName")[0].value;

	if (!checkInputLength()) {
		return;
	}

	var isExist = $("providerIsExist")[1].checked;
	var param = ("&isExist=" + isExist + "&writer=" + (up_doc
			.getElementById("my:总办填写表单_") ? 1 : 2));
	if (up_doc.getElementById("my:总办填写表单_") && isExist) {
		var typeNodes = typeTree.getAllChecked();
		var companyNodes = companyTree.getAllChecked();
		var code = $("query_providerCode")[0].value;
		param += ("&typeNodes=" + typeNodes + "&companyNodes=" + companyNodes
				+ "&code=" + code);
	}

	createAjax("method=checkProviderNameIsExist&providerName="
					+ (encodeURIComponent(providerName)) + param,
			"./oa2uapController.do", function(xmlhttp, hideLodingDiv) {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					hideLodingDiv();
					text = xmlhttp.responseText;
					var result = eval('(' + text + ')');
					if (result.success == 'false') {
						alert("数据库访问异常，无法验证用户名是否已存在");
						return;
					} else if ((!result.code || result.code == '' || result.code == document
							.getElementsByName("providerCode")[0].value)) {
						if (up_doc.getElementById("my:总办填写表单_") && isExist) {
							if (result.msg) {
								var companyNodeIDArray = companyTree
										.getAllChecked().split(',');
								var typeIDArray = typeTree.getAllChecked()
										.split(',');
								var message = '';
								var sign = false;
								if ($("opinion")[1].checked) {
									for (var cIndex in companyNodeIDArray) {
										for (var tIndex in typeIDArray) {
											if (typeIDArray[tIndex]
													&& typeIDArray[tIndex] != ''
													&& typeJSON[typeIDArray[tIndex]].isleaf == 'Y') {
												for (var mIndex in result.msg) {
													if (result.msg[mIndex].pk_corp == companyNodeIDArray[cIndex]
															&& result.msg[mIndex].pk_invclass == typeIDArray[tIndex]) {
														if (result.msg[mIndex].cvendorclass == 2) {
															sign = true;
														}
													}
												}
												if (!sign) {
													message += '\n';
													message += companyTree
															.getItemText(companyNodeIDArray[cIndex]);
													message += "      ";
													message += typeTree
															.getItemText(typeIDArray[tIndex]);
												}
												sign = false;
											}
										}
									}
									if (message != '') {
										message = "以下供货范围不是潜在的，不会导入供应商系统："
												+ message;
									}
								} else {
									for (var cIndex in companyNodeIDArray) {
										for (var tIndex in typeIDArray) {
											if (typeIDArray[tIndex]
													&& typeIDArray[tIndex] != ''
													&& typeJSON[typeIDArray[tIndex]].isleaf == 'Y') {
												for (var mIndex in result.msg) {
													if (result.msg[mIndex].pk_corp == companyNodeIDArray[cIndex]
															&& result.msg[mIndex].pk_invclass == typeIDArray[tIndex]
															&& result.msg[mIndex].cvendorclass != '1'
															&& result.msg[mIndex].cvendorclass != '2'
															&& result.msg[mIndex].cvendorclass != '3') {
														sign = true;
													}
												}
												if (sign) {
													message += '\n';
													message += companyTree
															.getItemText(companyNodeIDArray[cIndex]);
													message += "      ";
													message += typeTree
															.getItemText(typeIDArray[tIndex]);
												}
												sign = false;
											}
										}
									}
									if (message != '') {
										message = "以下供货范围已经是潜在/合格的，或者已被列入黑名单，不会导入供应商系统："
												+ message;
									}
								}
								if (message != '') {
									alert(message);
								}
							} else {
								alert("验证导入的供货范围失败");
							}
						}

						// opinion:["my:处理意见", "my:处理意见值"],
						// groupManage:["my:集团管控", "my:是否集团重点管控"],
						var contrast = inputMap.contrast;
						up_doc.getElementById(contrast["providerCode"]).is_null = "true";
						// up_doc.getElementById(contrast["providerCode"]).fireEvent('onchange');
						writeSupplyRangeToForm();
						writeImportCompanyNodeToForm();
						for (var key in contrast) {
							try {
								var node = $(key)[0];
								if (node.tagName.toUpperCase() == "SELECT") {
									if (node.selectedIndex != -1) {
										if (typeof contrast[key] == 'string') {
											up_doc
													.getElementById(contrast[key]).value = node.options[node.selectedIndex].text;
										} else {
											up_doc
													.getElementById(contrast[key][1]).value = node.value;
											up_doc
													.getElementById(contrast[key][0]).value = node.options[node.selectedIndex].text;
										}
									} else {
										if (typeof contrast[key] == 'string') {
											up_doc
													.getElementById(contrast[key]).value = "";
										} else {
											up_doc
													.getElementById(contrast[key][1]).value = "";
											up_doc
													.getElementById(contrast[key][0]).value = "";
										}
									}
								} else {
									if (node.type.toUpperCase() == "RADIO") {
										if (key == "opinion") {
											up_doc
													.getElementById(contrast[key][0]).value = node.checked
													? "导入潜在供应商名录"
													: "导入合格供应商名录";
											$2(contrast[key][1])[0].checked = node.checked;
											$2(contrast[key][1])[1].checked = $(key)[1].checked;
										} else {
											if (key == "groupManage") {
												up_doc
														.getElementById(contrast[key][0]).value = node.checked
														? "在集团重点管控物料类别清单内"
														: "不在集团重点管控类别清单内";
												$2(contrast[key][1])[0].checked = node.checked;
												$2(contrast[key][1])[1].checked = $(key)[1].checked;
											} else {
												if (key == "providerIsExist") {
													$2(contrast[key])[0].checked = $(key)[1].checked;
													$2(contrast[key])[1].checked = node.checked;
												}
											}
										}
									} else {
										up_doc.getElementById(contrast[key]).value = node.value;
									}
								}
								if (typeof contrast[key] == "string") {
									up_doc.getElementById(contrast[key])
											.fireEvent("onchange");
								} else {
									if (typeof contrast[key] == "array") {
										up_doc.getElementById(contrast[key][0])
												.fireEvent("onchange");
									}
								}
							} catch (e) {
								alert(e.description);
							}
						}
						writeFormApptitude();

						if (operator == 0) {
							returnValue = $("providerName")[0].value;
						} else if (operator == 1) {
							returnValue = $("providerCode")[0].value;
						}
						writeSuccess = true;
						window.close();
					} else {
						alert("供应商名称已存在，此供应商编码为：" + result.code);
					}
				}
			});
};
var onSupplierTypeRadioClick = function() {
	var value = $("providerIsExist")[1].checked;
	// 判断是否变更
	if (providerIsExist == null || providerIsExist != value) {
		providerIsExist = value;
		resetAptitude();
		if (providerIsExist) {
			setInputDisabled(inputMap.providerIsNotExist.handwritingInput);
			setInputReadonly(inputMap.providerIsExist.firstStep.redonly);
			setInputCanFill(inputMap.providerIsExist.firstStep.fill);
			document.getElementById("supplyRangeParentDIV").style.display = "none";
			document.getElementById("companyTreeParentDIV").style.display = "none";
			document.getElementById("queryTR").style.display = "";
			typeTree.setCheck(0, false);
			companyTree.setCheck(0, false);

			if (up_doc.getElementById("my:总办填写表单_")) {
				$("opinion")[0].checked = true;
				$("opinion")[1].disabled = true;
			}

			setCcontentAndCcuspaycondDisabled();
		} else {
			setInputDisabled(inputMap.providerIsExist.firstStep.fill);
			setInputDisabled(inputMap.providerIsExist.firstStep.redonly);
			document.getElementById("supplyRangeParentDIV").style.display = "";
			document.getElementById("companyTreeParentDIV").style.display = "";
			document.getElementById("queryTR").style.display = "none";
			setInputCanFill(inputMap.providerIsNotExist.handwritingInput);
			$("groupManage")[0].checked = true;

			$("opinion")[0].checked = true;
			$("opinion")[1].disabled = true;

			setCcontentAndCcuspaycondDisabled();
			showAptitude();
		}
	}
};

var createAjax = function(params, url, operator) {
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera,
		// Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		operator(xmlhttp, function() {
			window.parent.document.getElementById("lodingDiv").style.display = 'none';
		});
	};
	window.parent.document.getElementById("lodingDiv").style.height = window.parent.document.body.clientHeight
			+ 5 + "px";
	window.parent.document.getElementById("lodingDiv").style.display = '';
	xmlhttp.open("get", url + "?" + params + "&temp=" + Math.random(), true);
	xmlhttp.send();
};

var queryProviderInfo = function() {
	try {
		var value = document.getElementById("query_providerCode").value;
		value = value.replace(/ /g, '');
		document.getElementById("query_providerCode").value = value;
		if (value !== null && value !== "") {
			createAjax(("method=findProviderInfo&providerCode=" + value),
					"./oa2uapController.do", function(xmlhttp, hideLodingDiv) {
						if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
							text = xmlhttp.responseText;
							if (text == 1) {
								alert("用户已经掉线，请先登录");
							} else if (text == 2) {
								alert("供应商编码不能为空");
							} else if (text == 3) {
								alert("供应商编码输入错误，无此供应商或已被停用");
							} else {
								var obj = eval('(' + text + ')');
								document.getElementById("supplyRangeParentDIV").style.display = "";
								document.getElementById("companyTreeParentDIV").style.display = "";
								setInputCanFill(inputMap.providerIsNotExist.handwritingInput);
								$("opinion")[0].checked = true;
								$("groupManage")[0].checked = true;
								for (var key in obj) {
									if (key == "apptitude") {
										showApptitudeValues(obj.apptitude);
									} else {
										var dom = $(key)[0];
										if (typeof dom === 'object') {
											if (key == 'property') {
												dom.value = (obj[key] == 'D'
														? '0'
														: '1');
											} else {
												dom.value = obj[key];
											}
										}
									}
								}
								$("providerCode")[0].value = value;

								setCcontentAndCcuspaycond();

								// 合格供应商
								providerIsExistRange.qualifiedSuppliers = obj.qualifiedSuppliers;
								providerIsExistRange.potentialSuppliers = obj.potentialSuppliers;
								providerIsExistRange.stopSuppliers = obj.stopSuppliers;
								setNodeCheckWhenProviderIsExist();

							}
							hideLodingDiv();
						}

					});
		} else {
			alert("供应商编码不能为空.");
		}
	} catch (e) {
		alert(e);
	}
};
var setInputCanFill = function(nameArray) {
	for (var index in nameArray) {
		var inputs = $(nameArray[index]);
		for (var i = 0; i < inputs.length; i++) {
			if (inputs[i].type.toUpperCase() == "RADIO") {
				inputs[i].checked = false;
			} else {
				inputs[i].value = "";
				inputs[i].className = "manualInput";
			}
			inputs[i].readonly = false;
			inputs[i].disabled = false;
		}
	}
};
var setInputDisabled = function(nameArray) {
	for (var index in nameArray) {
		var inputs = $(nameArray[index]);
		for (var i = 0; i < inputs.length; i++) {
			if (inputs[i].type.toUpperCase() == "RADIO") {
				inputs[i].checked = false;
			} else {
				inputs[i].value = "";
				inputs[i].className = "disabledInput";
			}
			inputs[i].disabled = true;
		}
	}
};
var setInputReadonly = function(nameArray) {
	for (var index in nameArray) {
		var inputs = $(nameArray[index]);
		for (var i = 0; i < inputs.length; i++) {
			if (inputs[i].type.toUpperCase() == "RADIO") {
				inputs[i][0].checked = false;
			} else {
				inputs[i].value = "";
				inputs[i].className = "readOnlyInput";
			}
			inputs[i].disabled = false;
			inputs[i].readOnly = "readonly";
		}
	}
};

var clearAllInput = function() {
	// setInputDisabled(inputMap.contrast);
};
var onCcontentSelectChange = function(node) {
	if (node.value == 5) {
		document.getElementById("ccontentDiv").style.display = '';
		$('ccontent')[0].value = '';
	} else {
		document.getElementById("ccontentDiv").style.display = 'none';
		$('ccontent')[0].value = ccontentValueArray[node.value];
	}
};
var onCcuspaycondSelectChange = function(node) {
	if (node.value == 4) {
		document.getElementById("ccuspaycondDiv").style.display = '';
		$('ccuspaycond')[0].value = '';
	} else {
		document.getElementById("ccuspaycondDiv").style.display = 'none';
		$('ccuspaycond')[0].value = ccuspaycondValueArray[node.value];
	}
};

var inputMap = {
	providerIsExist : {
		firstStep : {
			redonly : ["providerCode"],
			fill : ["query_providerCode"]
		},
		readonlyInput : [],
		disabledInput : []
	},
	supplyRange : [["my:供货范围分类1", "my:供货范围分类1值"],
			["my:供货范围分类2", "my:供货范围分类2值"], ["my:供货范围分类3", "my:供货范围分类3值"]],
	importCompany : [["my:导入制造部名称", "my:导入制造部值"]],
	providerIsNotExist : {
		handwritingInput : ["providerName", "opinion", "groupManage", "reason",
				"similarSuppliers", "address", "linkman", "supplyCycle",
				"cvenbank", "supplyAddress", "tel", "cpurbillcode",
				"bankAccount", "taxRegister", "fax", "couttype", "cmoneytype",
				"qualitySystem", "postCode", "companyType", "ccuspaycond",
				"property", "email", "ccontent", "fax", 'ccuspaycondSelect',
				'ccontentSelect', 'cVenBankMemo', 'bankAccountName', 'bankCode','cvenfygl']
	},
	contrast : {
		providerName : "my:供应商名称",
		providerCode : "my:供应商编码",
		opinion : ["my:处理意见", "my:处理意见值"],
		groupManage : ["my:集团管控", "my:是否集团重点管控"],
		reason : "my:新增原因",
		similarSuppliers : "my:同类供应商",
		address : "my:企业地址",
		linkman : "my:联系人",
		supplyCycle : "my:供货周期",
		cvenbank : "my:结算银行",
		supplyAddress : "my:发货地址",
		tel : "my:电话号码",
		cpurbillcode : "my:发票税码",
		bankAccount : "my:银行账号",
		taxRegister : "my:税务登记号",
		fax : "my:传真号码",
		couttype : ["my:物流方式", "my:物流方式PK"],
		cmoneytype : ["my:币种", "my:币种PK"],
		qualitySystem : "my:质量体系",
		postCode : "my:邮编",
		companyType : ["my:企业类型", "my:企业类型PK"],
		ccuspaycond : "my:付款条件",
		property : "my:属性",
		email : "my:电子邮箱",
		ccontent : "my:价格条款",
		fax : "my:传真号码",
		providerIsExist : "my:是否在供应商名录内",
		cVenBankMemo : "my:开户银行",
		bankAccountName : 'my:银行开户名',
		bankCode : 'my:银行代码',
		cvenfygl : "my:是否福耀关联方"
	},
	inputLengthMap : {
		providerName : 100,
		providerCode : 20,
		address : 100,
		linkman : 20,
		supplyCycle : 20,
		cvenbank : 100,
		supplyAddress : 100,
		tel : 20,
		cpurbillcode : 20,
		bankAccount : 45,
		taxRegister : 20,
		fax : 20,
		qualitySystem : 20,
		postCode : 20,
		ccuspaycond : 100,
		property : 5,
		email : 100,
		ccontent : 100,
		fax : 20,
		cVenBankMemo : 100,
		bankAccountName : 96,
		bankCode : 20
	}
};

window.onunload = function() {
	if (!writeSuccess) {
		returnValue = function() {
		};
	}
}
